package com.lgmshare.component.imageloader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.GenericTransitionOptions;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.TransitionOptions;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.lgmshare.component.utils.UIUtils;

/**
 * 图片加载封装类库
 *
 * @author lim
 * @description: TODO
 * @email lgmshare@gmail.com
 * @datetime 2016/11/21 11:21
 */
public class ImageLoader {

    public static void load(@NonNull Context context, @NonNull ImageView imageView, @NonNull Object model) {
        load(context, imageView, model, null);
    }

    public static void load(@NonNull Context context, @NonNull ImageView imageView, @NonNull Object model, RequestOptions ro) {
        load(context, imageView, model, ro, null);
    }

    public static void load(@NonNull Context context, @NonNull ImageView imageView, @NonNull Object model, RequestOptions ro, TransitionOptions to) {
        RequestBuilder<Drawable> builder = Glide.with(context).load(model);
        if (null != ro) {
            builder.apply(ro);
        }
        if (null != to) {
            builder.transition(to);
        }
        builder.into(imageView);
    }

    /**
     * @param context
     * @param imageView 目标imageView
     * @param url       图片url
     * @param ph        占位图
     */
    public static void load(@NonNull Context context, @NonNull ImageView imageView, @NonNull String url, int ph) {
        RequestOptions options = getRequestOptions(ph);
        options.centerCrop();
        load(context, imageView, url, options);
    }

    /**
     * @param context
     * @param imageView 目标imageView
     * @param url       图片url
     * @param ph        占位图
     */
    public static void loadCircleCrop(@NonNull Context context, @NonNull ImageView imageView, @NonNull String url, int ph) {
        RequestOptions options = getRequestOptions(ph);
        options.centerCrop();
        options.circleCrop();
        load(context, imageView, url, options);
    }

    public static void loadRoundedCorners(@NonNull Context context, @NonNull ImageView imageView, @NonNull String url, int ph) {
        loadRoundedCorners(context, imageView, url, ph, 16);
    }

    public static void loadRoundedCorners(@NonNull Context context, @NonNull ImageView imageView, @NonNull String url, int ph, int radius) {
        RequestOptions options = getRequestOptions(ph);
        //同時寫入transform， 否則共用CenterCrop會覆蓋掉圓角設置
        options.transform(new CenterCrop(), new RoundedCorners(radius));
        load(context, imageView, url, options);
    }

    /**
     * @param context
     * @param imageView 目标imageView
     * @param url       图片url
     * @param w         图片宽度
     * @param h         图片高度
     * @param ph        占位图
     */
    public static void loadSize(@NonNull Context context, @NonNull ImageView imageView, @NonNull String url, int w, int h, int ph) {
        RequestOptions options = getRequestOptions(w, h, ph);
        load(context, imageView, url, options);
    }

    /**
     * @param context
     * @param imageView 目标imageView
     * @param url       图片url
     * @param w         图片宽度
     * @param h         图片高度
     * @param ph        占位图
     */
    public static void loadSizeToCenterCrop(@NonNull Context context, @NonNull ImageView imageView, @NonNull String url, int w, int h, int ph) {
        RequestOptions options = getRequestOptions(w, h, ph);
        options.override(w, h);
        options.centerCrop();
        load(context, imageView, url, options);
    }

    /**
     * @param context
     * @param imageView 目标imageView
     * @param url       图片url
     * @param ph        占位图
     */
    public static void loadSizeToFitCenter(@NonNull Context context, @NonNull ImageView imageView, @NonNull String url, int ph) {
        RequestOptions options = getRequestOptions(ph);
        options.fitCenter();
        load(context, imageView, url, options);
    }

    /**
     * @param context
     * @param imageView 目标imageView
     * @param url       图片url
     * @param w         图片宽度
     * @param h         图片高度
     * @param ph        占位图
     */
    public static void loadSizeToFitCenter(@NonNull Context context, @NonNull ImageView imageView, @NonNull String url, int w, int h, int ph) {
        RequestOptions options = getRequestOptions(w, h, ph);
        options.fitCenter();
        load(context, imageView, url, options);
    }

    public static void loadGif(@NonNull Context context, @NonNull ImageView imageView, @NonNull String url) {
        RequestOptions options = new RequestOptions();
        options.decode(GifDrawable.class);
        load(context, imageView, url, options);
    }

    /**
     * 加载为bitmap
     *
     * @param context
     * @param url      图片地址
     * @param listener 回调
     */
    public static void loadBitmap(@NonNull Context context, String url, final LoadBitmapListener listener) {
        Glide.with(context).asBitmap().load(url).listener(new RequestListener<Bitmap>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                if (listener != null) {
                    listener.onReady(resource);
                }
                return false;
            }
        });
    }

    public static RequestOptions getRequestOptions(int ph) {
        RequestOptions options = new RequestOptions();
        options.placeholder(ph).error(ph);
        return options;
    }

    public static RequestOptions getRequestOptions(int w, int h, int ph) {
        RequestOptions options = new RequestOptions();
        options.placeholder(ph).error(ph);
        options.override(w, h);
        return options;
    }

    /**
     * GenericTransitionOptions 通用型
     * DrawableTransitionOptions
     * BitmapTransitionOptions
     *
     * @return
     */
    public static TransitionOptions getTransitionOptions() {
        TransitionOptions options = BitmapTransitionOptions.withCrossFade();
        return options;
    }

    /**
     * @param viewAnimationId
     * @return
     */
    public static TransitionOptions getTransitionOptions(int viewAnimationId) {
        TransitionOptions options = GenericTransitionOptions.with(viewAnimationId);
        return options;
    }

    /**
     * 恢复请求
     * 当列表在滑动的时候，调用pauseRequests()取消请求，滑动停止时，调用resumeRequests()恢复请求
     *
     * @param context
     */
    public static void resume(Context context) {
        Glide.with(context).resumeRequests();
    }

    /**
     * 取消请求
     * 当列表在滑动的时候，调用pauseRequests()取消请求，滑动停止时，调用resumeRequests()恢复请求
     *
     * @param context
     */
    public static void pause(Context context) {
        Glide.with(context).pauseRequests();
    }

    /**
     * 请在主线程中调用
     *
     * @param context
     */
    public static void cleanMemory(Context context) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            Glide.get(context).clearMemory();
        } else {
            UIUtils.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Glide.get(context).clearMemory();
                }
            });
        }
    }

    /**
     * 加载bitmap回调
     */
    public interface LoadBitmapListener {
        void onReady(Bitmap bitmap);

        void onFailed();
    }
}
